# Maintainer: Christian Rebischke <chris.rebischke at archlinux.org>
# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor:  kpcyrd <git@rxv.cc>
# Contributor: Jonathan Steel <jsteel at archlinux.org>
# Contributor: Daniel Wallace <danielwallace at gtmanfred dot com>
# Contributor: flaccid aka Chris Fordham <chris@fordham.id.au>
# Contributor: Sparadox <etienne.lafarge at gmail.com>

pkgname=cloud-init
pkgver=23.3.3
pkgrel=1
pkgdesc="Cloud instance initialization"
arch=(any)
url="https://cloud-init.io"
license=('GPL-3.0-only OR Apache-2.0')
depends=(
  bash
  dhclient
  python
  python-configobj
  python-jinja
  python-jsonpatch
  python-jsonschema
  python-netifaces
  python-oauthlib
  python-pyserial
  python-pyyaml
  python-requests
  python-typing-extensions
  sudo
  systemd
)
makedepends=(
  netplan
  python-build
  python-installer
  python-setuptools
  python-wheel
)
checkdepends=(
  procps-ng
  python-httpretty
  python-passlib
  python-pytest
  python-pytest-mock
  python-responses
)
optdepends=(
  'cloud-guest-utils: for growpart'
  'netplan: for configuring network using netplan'
  'python-passlib: for Azure and BSD support'
  'python-urllib3: for LXD and Scaleway data sources'
)
backup=(
  etc/cloud/cloud.cfg
  etc/cloud/cloud.cfg.d/05_logging.cfg
)
source=(
  https://github.com/canonical/cloud-init/archive/$pkgver/$pkgname-$pkgver.tar.gz
  $pkgname-22.1-lib_dir.patch
)
sha512sums=('c07b61ea1d01e9977ccba269d73e22001cb433aca6be7d99086100a0d52abcf9fe2c09724c1fb551e44c4b1c82488e666dc6fed32ff26662bbfdb41d70915aa9'
            '32b7521f0ae4d61a4d86f2f840fb0670e2cdc7bab708fc7b7676b5178b6d660beb3a46b803f5bcd7e0f18c6d6339fc96e20a951dfafadbb0312e4a0cafce325f')
b2sums=('1f99fb3b762b05bf81ccc212111af49dcc6e89d542084618cd264597058ffdbead95c4028676a733c0113677fe3cd397b8b0201f54323be9f5f49e332723e25c'
        '4e03bd647ca9e1bc3d9c507824d43e4013a20d5bbcb7215755b427d0c7ded9ee48dbfb9faaae703ab2d519aee7c48a40277603c8f874347b574b7ac2b1e1a9fd')

prepare() {
  cd $pkgname-$pkgver
  # we use /usr/lib
  patch -Np1 -i ../$pkgname-22.1-lib_dir.patch
}

build() {
  cd $pkgname-$pkgver
  python -m build --wheel --no-isolation
}

check() {
  local deselected=(
    # we don't ship /etc/ca-certificates.conf
    --deselect tests/unittests/config/test_cc_ca_certs.py::TestRemoveDefaultCaCerts::test_commands
  )
  cd $pkgname-$pkgver
  pytest -vv "${deselected[@]}"
}

package() {
  local _file
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $pkgname-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  # NOTE: due to limitations with PEP517, files are installed to site-packages,
  # not to the correct global locations (e.g. /etc and /usr), so we remove them
  # and install them manually below.
  rm -frv "$pkgdir/$site_packages/"{usr,etc}

  # configuration and hooks
  install -vDm 644 config/clean.d/* -t "$pkgdir/etc/cloud/clean.d/"
  install -vDm 644 config/cloud.cfg.d/* -t "$pkgdir/etc/cloud/cloud.cfg.d/"
  ./tools/render-cloudcfg --variant arch ./config/cloud.cfg.tmpl "$pkgdir/etc/cloud/cloud.cfg"
  install -vDm 644 templates/*.tmpl -t "$pkgdir/etc/cloud/templates/"
  install -vDm 755 tools/{ds-identify,hook-hotplug,uncloud-init,write-ssh-key-fingerprints} -t "$pkgdir/usr/lib/$pkgname/"
  # documentation, man pages and shell completion
  install -vDm 644 doc/*.txt -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 doc/examples/*.txt -t "$pkgdir/usr/share/doc/$pkgname/examples/"
  install -vDm 644 doc/examples/seed/* -t "$pkgdir/usr/share/doc/$pkgname/examples/seed/"
  install -vDm 644 doc/man/*.1 -t "$pkgdir/usr/share/man/man1/"
  install -vDm 655 bash_completion/$pkgname -t "$pkgdir/usr/share/bash-completion/completions/"
  # udev rules
  install -vDm 644 udev/*.rules -t "$pkgdir/usr/lib/udev/rules.d/"
  # systemd integration
  install -vdm 755 "$pkgdir/usr/lib/systemd/system"{,-generators}
  for _file in cloud-{config,final,init-local,init}.service; do
    ./tools/render-cloudcfg --variant arch ./systemd/$_file.tmpl "$pkgdir/usr/lib/systemd/system/$_file"
  done
  install -vDm 644 systemd/*.{service,socket,target} "$pkgdir/usr/lib/systemd/system/"
  ./tools/render-cloudcfg --variant arch ./systemd/cloud-init-generator.tmpl "$pkgdir/usr/lib/systemd/system-generators/cloud-init-generator"
  chmod 755 "$pkgdir/usr/lib/systemd/system-generators/cloud-init-generator"
  install -vDm 644 systemd/disable-sshd-keygen-if-cloud-init-active.conf -t "$pkgdir/usr/lib/systemd/system/sshd-keygen@.service.d/"
}
